package android.cs2340UDT;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class MapScreenActivity extends MapActivity {
	
	Manager manage = Security.getManager();
	ArrayList<ItemLocation> items = manage.getItemLocations();
	
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.map_screen);
	    MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    MapController mc = mapView.getController(); 
	    
	    ImageButton BackButton = (ImageButton) findViewById(R.id.Back);
	    
	    
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
	    LocationOverlay locations = new LocationOverlay(drawable, this);

	    
	    BackButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent nextScreen = new Intent(getApplicationContext(), MainScreenActivity.class);
				startActivity(nextScreen);
			}
		});
	    
	    
	    Geocoder geocode = new Geocoder(this, Locale.getDefault());
	    try{
	    	for(int b=0; b<items.size(); b++)
	    	{
	    		ItemLocation newLocation = items.get(b);
	    		List<Address> addresses = geocode.getFromLocationName(newLocation.getAddress(), 3);
	            String add = "";
	            if (addresses.size() > 0) {
	                GeoPoint point = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6));
	                OverlayItem over = new OverlayItem(point, newLocation.getTitle(), newLocation.getComment());
	                locations.addOverlay(over);
	            }
	    	}
	        } catch (IOException e) {
	            e.printStackTrace();
	    }
	    
	    
        
        //VERY IMPORTANT LINE HERE THIS MUST BE LAST
        mapOverlays.add(locations);
	}
	
}


